Fix VAE offload encode device mismatch in DreamBooth scripts#13417
Merged
sayakpaul merged 2 commits intohuggingface:mainfrom Apr 6, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a device/dtype mismatch when VAE offloading is enabled in DreamBooth LoRA training example scripts by ensuring pixel_values (and conditional pixel values for img2img variants) are moved to accelerator.device before calling vae.encode(...) in the non-latent-cached path.
Changes:
- Move
batch["pixel_values"]todevice=accelerator.device(in addition todtype=vae.dtype) before VAE encoding underoffload_models(...). - For img2img variants, also move
cond_pixel_valuestoaccelerator.deviceand ensure bothvae.encode(...)calls occur inside the offload context.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| examples/dreambooth/train_dreambooth_lora_z_image.py | Moves pixel_values to accelerator.device before vae.encode in the non-cached path. |
| examples/dreambooth/train_dreambooth_lora_flux2.py | Same device move for Flux2 DreamBooth LoRA training when not using cached latents. |
| examples/dreambooth/train_dreambooth_lora_flux2_klein.py | Same device move for Flux2 Klein variant when not using cached latents. |
| examples/dreambooth/train_dreambooth_lora_flux2_klein_img2img.py | Moves both pixel_values and cond_pixel_values to accelerator.device and encodes inside offload context. |
| examples/dreambooth/train_dreambooth_lora_flux2_img2img.py | Moves both pixel_values and cond_pixel_values to accelerator.device and encodes inside offload context. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Member
|
Failing tests are unrelated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a device mismatch in the Flux2 DreamBooth training scripts when
--offloadis used and latents are not precomputed.non-cached path:
pixel_valueswere converted tovae.dtypebut not moved ontoaccelerator.devicebeforevae.encode(...), which can leave the inputs and VAE weights on different devices under offload.Reproduction:
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the sameBefore submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@sayakpaul